package com.guigu.eduservice.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.guigu.common.utils.R;
import com.guigu.eduservice.entity.Equipment;
import com.guigu.eduservice.entity.EquipmentReservation;
import com.guigu.eduservice.entity.Order;
import com.guigu.eduservice.service.EquipmentReservationService;
import com.guigu.eduservice.service.EquipmentService;
import com.guigu.eduservice.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.guigu.eduservice.service.EquipmentReservationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/eduservice/equipmentReservation")
public class EquipmentReservationController {

    @Autowired
    private EquipmentReservationService equipmentReservationService;
    @Autowired
    private EquipmentService equipmentService;
    @Autowired
    private OrderService orderService;

    @GetMapping("/getAllByUId/{uId}")
    public R getAllByUId(@PathVariable Long uId){
        //需要判断时间是否过期
        List<EquipmentReservation> list = equipmentReservationService.selectAllByUserId(uId);
        return R.ok().data("list",list);
    }
    @DeleteMapping("/deleteById/{id}")
    public R deleteById(@PathVariable("id") Long id){
        EquipmentReservation equipmentReservation = equipmentReservationService.getById(id);
        if (equipmentReservation==null){
            return R.error();
        }
        boolean delete = equipmentReservationService.removeById(id);
        if ( delete){
            //如果删除成功，那我应该将设备的状态改为正常
            Equipment equipment = equipmentService.getById(equipmentReservation.getEquipmentId());
            equipment.setStatus(1);
            boolean b = equipmentService.updateById(equipment);
            if (!b)return R.error();
            boolean b1 = orderService.removeById(equipmentReservation.getOrderId());
            if (!b1)return R.error();
        }
        return delete ? R.ok() : R.error();
    }


}
